import { Config } from "../Config";
import { GameMgr } from "../game/GameMgr";
import { Watcher } from "../game/other/Watcher";
import MySQLService from "../service/MySQLService";
import Logger from "../utils/Logger";
import HallMgr from "./HallMgr";
import { User } from "./User";

export enum RoomState {
    MATCHING,       //匹配中
    GAMEING,        //游戏中
    CLOSE           //已关闭
}

export class Room {

    id: number = -1;
    cipher: string = null;    //房间暗号 四个数字，string类型
    users: Array<User> = new Array<User>();       //玩家
    state: RoomState = RoomState.MATCHING;           //房间状态
    gameMgr: GameMgr;                                //游戏管理者

    constructor(id: number, cipher: string = null) {
        this.id = id;
        this.cipher = cipher;
        Logger.log("房间创建：id = " + id + ",cipher = " + cipher);
    }

    /** 是否可以加入房间 */
    checkAddPlayer() {
        return this.state == RoomState.MATCHING
    }


    /** 检查密码房能否加入玩家 */
    checkCipherRoomAddPlayer(cipher: string) {
        return cipher == this.cipher && this.state == RoomState.MATCHING
    }

    /** 添加玩家 */
    addPlayer(user: User) {
        this.users.push(user);
        user.room = this;
        if (Config.DEBUG || this.users.length == 3) {
            this.startGame()
        }
    }

    /** 添加观众 */
    addWatcher(watcher: User) {
        Logger.log("添加观众");
        new Watcher(this.gameMgr.manual, watcher)
    }

    /** 开始游戏 */
    startGame() {
        Logger.log("房间号:", this.id, "开始游戏");
        this.state = RoomState.GAMEING
        HallMgr.updateOnline()
        this.gameMgr = new GameMgr(this, this.users)
        for (let user of this.users) {
            MySQLService.updateUserRoomId(user.username, this.id)
        }
    }

    /** 房间解散 */
    closeRoom() {
        this.state = RoomState.CLOSE;
        this.cipher == null ? HallMgr.rooms.delete(this) : HallMgr.cipherRooms.delete(this);
        this.gc()
        Logger.log("房间解散");
    }

    /** 内存清空 */
    gc() {
        this.gameMgr?.gc()
        this.gameMgr = null
        this.users.forEach(user => {
            user.room = null;
            user.player = null;
            user.watcher = null;
            MySQLService.updateUserRoomId(user.username, 0)
         })
        this.users = null
    }

    /** 取消匹配 */
    cancelMatch(user: User) {
        if (this.state == RoomState.MATCHING) {
            for (let i = 0; i < this.users.length; i++) {
                if (this.users[i] == user) {
                    this.users.splice(i, 1);
                }
            }
            if (this.users.length == 0) {
                this.cipher == null ? HallMgr.rooms.delete(this) : HallMgr.cipherRooms.delete(this);
                this.closeRoom();
            }
            user.room = null;
            Logger.log("取消匹配");
        } else {
            Logger.log("取消匹配失败");
        }
    }

    /** 玩家掉线，检查一下是不是全掉线了 */
    checkPlayersOnLineState(user: User) {
        if (Config.DEBUG) {
            this.closeRoom()
            return
        }
        // 还没写掉线重连逻辑，掉线相当于死亡
        user.player?.small?.die(user.player)
        // let online = 0
        // for (let player of this.users) {
        //     if(player.is_onLine) {
        //         online++
        //     }
        // }
        // if(online == 0) {
        //     this.closeRoom()
        // }
    }

    /** 认输 */
    admitDefeat(user: User) {
        user.player.small.die(user.player)
    }

}